﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

using BusinessFacade;
using EntityManage;
using Microsoft.Reporting.WebForms;

public partial class Modules_Personal_rptPerTargetScore : System.Web.UI.Page
{
    IPerf_Personal_EvaluateHandler evaluateHandler = new Perf_Personal_EvaluateManage();
    IPerf_Personal_EvaluateDtlHandler evaluateDtlHandler = new Perf_Personal_EvaluateDtlManage();

    ISys_OrganizationHandler organizationHandler = new Sys_OrganizationManage();

    IPerf_Personal_TargetOwnerHandler targetOwnerHandler = new Perf_Personal_TargetOwnerManage();
    DataBase db = new DataBase();

    ISys_UserHandlerExt userHandlerExt = new Sys_UserManageExt();
    IPerf_Personal_EvaluateHandlerExt evaluateHandlerExt = new Perf_Personal_EvaluateManageExt();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();

    IPerf_Personal_ApprovalHandler erfApprovalHandler = new Perf_Personal_ApprovalManage();
    IReportHandler rptHandler = new ReportManage();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDDl();
        }
    }

    private void FillDDl()
    {
        DataTable dt = rptHandler.GetQuarterList();
        ddlTitle.DataSource = dt;
        ddlTitle.DataTextField = "Title";
        ddlTitle.DataValueField = "Title";
        ddlTitle.DataBind();
        ddlTitle.Items.Insert(0, new ListItem(string.Empty, string.Empty));

        DataTable dtType = rptHandler.GetDistinctTargetType();

        ddlTargetType.DataSource = dtType;
        ddlTargetType.DataTextField = "Name";
        ddlTargetType.DataValueField = "ID";
        ddlTargetType.DataBind();
        // ddlTitle.Items.Insert(0, new ListItem(string.Empty, string.Empty));

    }

    #region Preview Report
    private void PreviewReport()
    {
        string whereClause = " 1=1 ";
        SqlParameter[] parms = null;
        ProcessParms(ref whereClause, ref parms);

        rptViewer.LocalReport.ReportPath = "Modules/Personal/rptPerTargetScore.rdlc";

        rptViewer.LocalReport.DataSources.Clear();

        DataTable dtNew = new DataTable();
       // dtNew.Columns.Add("TargetName");       
       // dtNew.Columns.Add("TotalScore", Type.GetType("System.Double"));
       
      
        if (whereClause != string.Empty)
        {
            whereClause += " and E.Target_User_ID=" + int.Parse(Session["CurrentUserID"].ToString());
        }

        dtNew = rptHandler.GetPerTargetScore(whereClause, parms);

       

        if (dtNew != null && dtNew.Rows.Count > 0)
        {
            rptViewer.Visible = true;
            lblNoRecord.Visible = false;
            ReportDataSource datasource = new ReportDataSource("rptDsPerTargetScore_DataTable1", dtNew);
            rptViewer.LocalReport.DataSources.Add(datasource);

            ReportParameter[] parameters = new ReportParameter[4];
            parameters[0] = new ReportParameter("DateFrom", DateTime.Now.ToShortDateString());
            parameters[1] = new ReportParameter("DateTo", DateTime.Now.ToShortDateString());
            parameters[2] = new ReportParameter("ReportTitle", "个人指标得分");
            parameters[3] = new ReportParameter("RptName", "个人指标得分");
            rptViewer.LocalReport.SetParameters(parameters);
            rptViewer.LocalReport.Refresh();
        }
        else
        {
            rptViewer.Visible = false;
            lblNoRecord.Visible = true;
        }
    }


    #endregion


    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();


        if (ViewState["Title"] != null && ViewState["Title"].ToString() != string.Empty)
        {
            whereClause += " and Title =@Title ";
            lstParm.Add(new SqlParameter("@Title", ViewState["Title"].ToString()));
        }

        if (ViewState["TypeID"] != null && ViewState["TypeID"].ToString() != string.Empty)
        {
            whereClause += " and T.TargetType_ID =@TypeID ";
            lstParm.Add(new SqlParameter("@TypeID", ViewState["TypeID"].ToString()));
        }

        parms = lstParm.ToArray();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        ViewState["Title"] = ddlTitle.SelectedValue;
        ViewState["TypeID"] = ddlTargetType.SelectedValue;
        PreviewReport();
    }
}
